﻿<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Collections.Generic;
using System.Web;
using BZV.Core;
using BZV.Foundation;
using BZV.Foundation.Business;
using BZV.Core.Net;

public class Handler : IHttpHandler {
    
    public class LotteryRecord
    {
        public string issue { get; set; }
        public string code { get; set; }
        public string status { get; set; }
    }
    
    public class LotteryRecordInfos
    {
        public string title { get; set; }
        public List<LotteryRecord> datas { get; set; }
    }
    
    /// <summary>
    /// 组态的预测记录
    /// </summary>
    public class CombinForcastRecord
    {
        public int first { get; set; }
        public int second { get; set; }
        public int cur { get; set; }
        public Dictionary<int, int> values;
    }
    
    public class NumberForcastRecord
    {
        public int num { get; set; }
        public int first { get; set; }
        public int second { get; set; }
        public int cur { get; set; }
        public Dictionary<int, int> values;
    }
    
    public void ProcessRequest (HttpContext context) {
        // 设置返回
        context.Response.ContentType = "application/json";//text/html";
        context.Response.Charset = "utf-8";
        
        // 获取查询方法
        string strMethod = context.Request.Form["method"];
        if(strMethod == null || strMethod == "")
        {
            strMethod = context.Request.QueryString["method"];
            if(strMethod == null || strMethod == "")
            {
                strMethod = "querylotterys";
            }
            
        }
        
        // 获取查询的索引号
        string strIndex = context.Request.Form["index"];
        int nIndex = 0;
        if (strIndex == null || strIndex == "")
        {
            strIndex = context.Request.QueryString["index"];
            if(strIndex == null || strIndex == "")
            {
                strIndex = "0";
            }
            
        }
        nIndex = Convert.ToInt32(strIndex);
        
        // 不同方法得到不同的处理方式
        string strJson = "";
        switch(strMethod)
        {
            case "querylotterys":
                // 变量处理
                string strCount = context.Request.Form["count"];
                if (strCount == null || strCount == "")
                {
                    strCount = context.Request.QueryString["count"];
                }
                int nCount = Convert.ToInt32(strCount);
                if (nCount <= 10)
                {
                    nCount = 10;
                }
                strJson = GetLastestIssues(nCount, nIndex);
                break;
                
            case "querycombin":
                strJson = GetForcastCombin(nIndex);
                break;
            case "querynumber":
                strJson = GetForcastNumber(nIndex);
                break;
        }
        
        // 发送数据
        context.Response.Write(strJson);
        context.Response.End();
    }
    
    public string GetLastestIssues(int nCount, int nIndex)
    {
        // 读取数据得到数据
        List<Lottery> lotterys = DataPool.GetDataOld();
        LotteryRecordInfos infos = new LotteryRecordInfos();
        infos.datas = new List<LotteryRecord>();
        infos.title = "数据";
        for (int i = nIndex; i < nIndex + nCount; i++)
        {
            Lottery lo = lotterys[i];
            LotteryRecord record = new LotteryRecord();
            record.issue = lo.Issue;
            record.code = lo.Code;
            record.status = lo.Param.comState.ToString();
            
            infos.datas.Add(record);
        }
        string strJson = JsonHelper.JsonSerializer<LotteryRecordInfos>(infos);
        return strJson;
    }
    
    
    /// <summary>
    /// 获得预测到的组态信息
    /// </summary>
    /// <param name="nCount">需要预测的组态数目</param>
    /// <returns></returns>
    public string GetForcastCombin(int nIndex)
    {
        // 创建json对象
        List<Lottery> lotterys = DataPool.GetDataOld();
        CombinForcastRecord record = new CombinForcastRecord();
        record.values = new Dictionary<int, int>();

        // 组态预测
        LotteryComPerdictor p = new LotteryComPerdictor();
        p.SetData(lotterys);
        int[] arr = p.ForecastCombinSix(nIndex);
        
        string str = "组态预测\n";
        Analyzer an = new Analyzer();
        an.SetData(lotterys);
        UintParam param = an.GetUintIndex(nIndex, 15);
        int nSix = param.CombinationCount[CombinationState.EN_COM_SIX];

        UintParam lastParam = an.GetUintIndex(nIndex + 20 + 15);
        UintParam curParam = an.GetUintIndex(nIndex + 15);
        record.first = lastParam.CombinationCount[CombinationState.EN_COM_SIX];
        record.second = curParam.CombinationCount[CombinationState.EN_COM_SIX];
        record.cur = param.CombinationCount[CombinationState.EN_COM_SIX];
        for (int i = 0; i < arr.GetLength(0); i++)
        {
            record.values.Add(i, arr[i]);
        }

        string strJson = JsonHelper.JsonSerializer<CombinForcastRecord>(record);
        
        return strJson;
    }

    /// <summary>
    /// 获得预测到的数字信息
    /// </summary>
    /// <returns></returns>
    public string GetForcastNumber(int nIndex)
    {
        // 数字预测
        List<Lottery> lotterys = DataPool.GetDataOld();
        LotteryComPerdictor p = new LotteryComPerdictor();
        p.SetData(lotterys);
        Analyzer an = new Analyzer();
        an.SetData(lotterys);

        List<NumberForcastRecord> records = new List<NumberForcastRecord>();
        for (int i = 0; i < 10; i++)
        {
            NumberForcastRecord record = new NumberForcastRecord();
            record.values = new Dictionary<int, int>();
            int[] NumArr = p.ForecastNum(nIndex, i);



            // 当前15个数字的个数
            record.num = i;
            record.cur = an.GetNumCount(nIndex, 15)[i];
            record.first = an.GetNumCount(nIndex + 20 + 15)[i];
            record.second = an.GetNumCount(nIndex + 15)[i];
            for (int j = 0; j < NumArr.GetLength(0); j++)
            {
                record.values.Add(j, NumArr[j]);
            }

            records.Add(record);
        }

        string strJson = JsonHelper.JsonSerializer<List<NumberForcastRecord>>(records);

        return strJson;
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}